import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";
import router from "@/router";
import i18n from "../../../../i18n";

const js3100 = {
    namespaced: true,
    state: {
        // table数据
        dataSource: [],

        // 查询条件
        queryForm: {
            userNameOrUserId: undefined,
            deptId: undefined,//部门
            status: 1,
            perssion: undefined,
        },
        statusFormList: [
            {
                id: 1,
                label: '正常',
            },
            {
                id: 0,
                label: '停用',
            }
        ],
        // 用户名/工号列表
        userNameList: [],
        loadTable: false,
        // 编辑表单数据
        capForm: {
            userName: undefined,
            deptId: undefined,//部门
            sectionId: undefined,//课别
            entryDate: undefined,
            status: undefined,
            resetPwd: 0//重置密码 1-重置
        },
        // 部门列表
        deptList: [],
        // 课列表
        sectionList: [],
        // 新增、编辑flag
        isFlag: null,
        // 新增编辑弹窗是否弹出
        capvisible: false,
        // 分配角色多选框选中值
        checkedValueList: [],
        // 角色列表
        checkedListByRole: [],
        // 角色弹窗是否弹出
        controlVisible: false,
        // table选中行
        selectRow: {},
        dataPagination:{
            pageSize:14,
            size:'small',
            total: 0,
            showTotal: (total, range) => {
                return i18n.t('menuItem.total')+` ${total} `+i18n.t('menuItem.term')
            },
        }
    },
    mutations: {
        setDataPagination(state, val) {
            state.dataPagination = val;
        },
        //查询table
        queryUser(state) {
            let codeInTrx = {
                trxId: "sysUser",
                actionFlg: "Q",
                ...state.queryForm
            };
            state.dataSource = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSource = outTrx.oaryA.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                    state.dataPagination.total = outTrx.tblCnt;
                }
            }).finally(() => {
                state.loadTable = false
            });
        },
        // 查询用户名/工号下拉框数据
        queryUserNameList(state) {
            let intrx = {
                trxId: "sysUser",
                actionFlg: "QU",
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.userNameList = outTrx.oaryB
                }
            })
        },
        // 查询用部门、课数据
        queryDeptAndSectionList(state) {
            let intrx = {
                trxId: "sysUser",
                actionFlg: "QD",
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.deptList = outTrx.deptList
                }
            })
        },
        // 查询分配角色
        queryRole(state) {
            let codeInTrx = {
                trxId: "sysRole",
                actionFlg: "Q",
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.checkedListByRole = outTrx.oaryA
                }
            }).finally(() => {
            });
        },
        // 查询用户已绑定的角色
        queryCheckRole(state, val) {
            let codeInTrx = {
                trxId: "sysUser",
                actionFlg: "QB",
                id: val.id
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.checkedValueList = outTrx.oaryC.map((item) => {
                        return item.roleId;
                    });
                }
            }).finally(() => {
            });
        },
        // 点击分配权限按钮
        assignPermission(state, val) {
            state.selectRow = val
            js3100.mutations.queryRole(state)
            js3100.mutations.queryCheckRole(state, val)
            state.controlVisible = true
        },
        // 清空弹窗数据
        cleanForm(state) {
            state.capForm = {
                userName: undefined,
                deptId: undefined,
                sectionId: undefined,
                entryDate: undefined,
                status: undefined,
                resetPwd: state.capForm.resetPwd
            }
            state.sectionList = []
        },
        // 分配角色弹窗确认，调用接口
        disthandleOk(state) {
            let codeInTrx = {
                trxId: "sysUser",
                actionFlg: "A",
                roleIdList: state.checkedValueList.toString(),
                id: state.selectRow.id,
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    js3100.mutations.cleancheckedValue(state)
                    state.controlVisible = false
                    message.success("Assign Perssion Success!")
                    // 分配角色用户为当前用户刷新页面
                    const username = window.localStorage.getItem("userIdMes");
                    if (username == state.selectRow.userName) {
                        router.go(0)
                    }
                }else{
                    message.success("Assign Perssion Fail!")
                }
            }).finally(() => {
            });
        },
        // 分配角色弹窗取消
        disthandleCancel(state) {
            state.controlVisible = false
            js3100.mutations.cleancheckedValue(state)
        },
        // 分配角色选中的数据
        setCheckedKeys(state, val) {
            state.checkedValueList = val
        },
        // 清空分配角色选中的数据
        cleancheckedValue(state) {
            state.checkedValueList = []
        },
    },
    getters: {},
    action: {}
}
export default js3100;
